package com.ars.order;

import com.ars.db.DBTools;
import com.ars.main.StageCollection;
import com.ars.main.User;
import com.sun.crypto.provider.RSACipher;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

import javax.swing.*;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ResourceBundle;

public class OrderController implements Initializable {
    //座位类型
    private static final int FIRST_SEATS = 1;
    private static final int BUSINESS_SEATS = 2;
    private static final int ECONOMY_SEATS = 3;
    //票种类型
    private static final int NORMAL_TICKET = 1;
    private static final int SPECIAL_TICKET = 2;
    @FXML
    private TextField tfPassengerName;
    @FXML
    private TextField tfPassengerId;
    @FXML
    private ComboBox<String> cbTicketType;
    @FXML
    private ComboBox<String> cbSeatsType;
    @FXML
    private TextField tfFlightName;
    @FXML
    private Label textPrice;
    //全票价格
    private String fullPrice;
    //数据库所需
    private String passengerName;
    private String passengerID;
    private int ticketType;
    private int seatsType;
    private String flightId;
    private int websiteId;
    private int seatsRemain;

    @FXML
    public void addButtonAction() {
        if (this.getFormValue()) {
            this.updateDB();
            JOptionPane.showMessageDialog(null, "购票成功！");
            StageCollection.getInstance().get("queryResultStage").close();
            StageCollection.getInstance().get("orderStage").close();
        }
    }

    @FXML
    public void cancelButtonAction() {
        StageCollection.getInstance().get("orderStage").close();
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        cbSeatsType.getItems().add("经济舱");
        cbSeatsType.getItems().add("商务舱");
        cbSeatsType.getItems().add("头等舱");
        cbSeatsType.setValue(resources.getString("seatType"));
        cbTicketType.getItems().add("普通票");
        cbTicketType.getItems().add("特种票");
        cbTicketType.setValue("特种票");
        tfFlightName.setText(resources.getString("flightName"));
        flightId = resources.getString("flightID");
        seatsRemain = Integer.parseInt(resources.getString("seatsRemain"));
        this.fullPrice = resources.getString("price");
        updatePrice();
    }

    //更新价格
    @FXML
    private void updatePrice() {
        double price = Integer.parseInt(fullPrice);
        if (cbSeatsType.getValue().equals("经济舱")) {
            price = price * 0.5;
        } else if (cbSeatsType.getValue().equals("商务舱")) {
            price = price * 0.7;
        }

        if (cbTicketType.getValue().equals("特种票")) {
            price = price * 0.5;
        }
        textPrice.setText(price + "");
    }

    //获取网点id
    private int getWebsiteId() {
    	int id = 0;
    	String name = User.getName();
    	String sql = "select * from website where name='"+name+"'";
    	ResultSet rs = new DBTools().query(sql);
    	try {
			if(rs.next()){
				id = rs.getInt("wid");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
System.out.println("hao"+id);
        return id;
    }

    //获取座位类型
    private int getSeatsType() {
        String seatsTypeString = cbSeatsType.getValue();
        switch (seatsTypeString) {
            case "头等舱":
                return FIRST_SEATS;
            case "商务舱":
                return BUSINESS_SEATS;
            case "经济舱":
                return ECONOMY_SEATS;
        }
        return -1;
    }

    //获取票种
    private int getTicketType() {
        String ticketTypeString = (String) cbTicketType.getValue();
        switch (ticketTypeString) {
            case "普通票":
                return NORMAL_TICKET;
            case "特种票":
                return SPECIAL_TICKET;
        }
        return -1;
    }

    //获取表单数据
    private boolean getFormValue() {
        if (tfPassengerName.getText() == null) {
            JOptionPane.showMessageDialog(null, "用户名不能为空！");
            return false;
        } else if (tfPassengerId.getText() == null) {
            JOptionPane.showMessageDialog(null, "请填写身份证号！");
            return false;
        }
        passengerName = tfPassengerName.getText();
        passengerID = tfPassengerId.getText();
        websiteId = getWebsiteId();
        ticketType = getTicketType();
        seatsType = getSeatsType();
        return true;
    }

    //更新数据库
    private void updateDB() {
        DBTools db = new DBTools();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String sql = "INSERT INTO orders VALUES(NULL,'" + passengerName + "','" + passengerID + "'," + seatsType + "," + ticketType + ",'" + df.format(new Date()) + "'," + websiteId + "," + flightId + ")";
        db.update(sql);
        if (seatsType == FIRST_SEATS) {
            sql = "UPDATE flight SET first_seats_remain = " + (seatsRemain - 1) + " WHERE fid = " + flightId + ";";
        } else if (seatsType == BUSINESS_SEATS) {
            sql = "UPDATE flight SET business_seats_remain = " + (seatsRemain - 1) + " WHERE fid = " + flightId + ";";
        } else {
            sql = "UPDATE flight SET economy_seats_remain = " + (seatsRemain - 1) + " WHERE fid = " + flightId + ";";
        }
        db.update(sql);
        System.out.println(sql);
    }

}
